package com.fh.wanandroid.ui.wx.presenter;

import android.util.Log;

import com.fh.wanandroid.net.RetrofitHelper;
import com.fh.wanandroid.ui.mainpager.MainPagerContract;
import com.fh.wanandroid.ui.mainpager.bean.FeedArticleListData;
import com.fh.wanandroid.ui.wx.contract.WXDetailArticleContract;
import com.fh.wanandroid.utils.NetErrorUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class WXDetailArticlePresenter extends  WXDetailArticleContract.Presenter{

    //分页数
    private int num;

    @Override
    public void loadInitData(int id) {

        num = 0;
        mCompositeDisposable.add(RetrofitHelper.createArticleApi().getWxSumData(id,num)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FeedArticleListData>() {
                    @Override
                    public void accept(FeedArticleListData articleBean) throws Exception {
//                        mView.refreshArticles(articleModel.getItems());
//                        mView.appendData(girlBean.getResults());
                        mView.refreshArticle(articleBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable t) throws Exception {
//                        mView.loadArticlesFail(NetErrorUtil.handleThrowable(t));
                        mView.loadDataFail(NetErrorUtil.handleThrowable(t));
                    }
                }));


    }

    @Override
    public void loadMoreData(int id) {

        mCompositeDisposable.add(RetrofitHelper.createArticleApi().getWxSumData(id,++num)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FeedArticleListData>() {
                    @Override
                    public void accept(FeedArticleListData articleBean) throws Exception {
//                        mView.refreshArticles(articleModel.getItems());
//                        mView.appendData(girlBean.getResults());
                        mView.appendArticle(articleBean);
//                        Log.e("WXDetailArticle","------------------WXDetailArticle:"+articleBean.getData().toString());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable t) throws Exception {
//                        mView.loadArticlesFail(NetErrorUtil.handleThrowable(t));
                        mView.loadDataFail(NetErrorUtil.handleThrowable(t));
                    }
                }));

    }
}
